package org.darcy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.darcy.dao.SysRoleMapper;
import org.darcy.entity.SysRole;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.model.SysRoleModel;
import org.darcy.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 角色
 *
 */
@Service
public class SysRoleServiceImpl extends GenericServiceImpl<SysRole> implements SysRoleService {

	@Autowired
	private SysRoleMapper roleMapper;

	/**
	 * 获取ztree使用的角色列表
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> queryRoleListWithSelected(Long userId) {
		List<SysRoleModel> sysRole = roleMapper.queryRoleListWithSelected(userId);
		if (CollectionUtils.isEmpty(sysRole)) {
			return null;
		}
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (SysRoleModel role : sysRole) {
			map = new HashMap<String, Object>(3);
			map.put("id", role.getId());
			map.put("pId", 0);
			map.put("checked", role.getSelected() != null && role.getSelected() == 1);
			map.put("name", role.getDescription());
			mapList.add(map);
		}
		return mapList;
	}

	@Override
	public IPage<SysRole> getPage(IPage<SysRole> page, QueryWrapper<SysRole> queryWrapper) {
		return dao.selectPage(page, queryWrapper);
	}
	
	

	/**
	 * 获取用户的角色
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<SysRoleModel> listRolesByUserId(Long userId) {
		List<SysRoleModel> sysRoles = roleMapper.listRolesByUserId(userId);
		return sysRoles;
	}

}
